<template>
  <div class="app-container">
    <div class="filter-container query">
      <el-form :model="listQuery" label-position="right" label-width="120px">
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="企业名称" prop="companyName">
              <el-input
                v-model="listQuery.companyName"
                @keyup.enter.native="handleFilter"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="站点名称" prop="stationName">
              <el-input
                v-model="listQuery.stationName"
                @keyup.enter.native="handleFilter"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="箱包状态" prop="bagsStatus">
              <el-select
                v-model="listQuery.bagsStatus"
                clearable
                filterable
                placeholder="请选择箱包状态"
              >
                <el-option
                  v-for="item in bagsStatusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="提交人" prop="payer">
              <el-input
                v-model="listQuery.payer"
                @keyup.enter.native="handleFilter"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-form-item label="订单编号" prop="orderNo">
              <el-input
                v-model="listQuery.orderNo"
                @keyup.enter.native="handleFilter"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="12" :lg="8">
            <el-form-item
              label="创建时间"
              prop="createAt"
              @keyup.enter.native="handleFilter"
            >
              <el-date-picker
                v-model="listQuery.createAt"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="6">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="handleFilter"
            >
              搜索
            </el-button>
            <el-button
              type="info"
              icon="el-icon-refresh"
              size="small"
              @click="handleReset"
            >
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <el-table
      :loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%; height: 100%"
      size="small"
      @sort-change="sortChange"
    >
      <el-table-column label="操作" align="center" width="80">
        <template slot-scope="{ row }">
          <el-button size="mini" type="primary" @click="handleDetail(row)">
            详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="企业名称"
        prop="companyName"
        width="200"
        header-align="center"
        align="left"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.companyName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="站点名称"
        prop="stationName"
        width="150"
        header-align="center"
        align="left"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.stationName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="箱包状态"
        prop="bagsStatus"
        :formatter="formatter"
        width="150"
        header-align="center"
        align="left"
      />
      <el-table-column
        label="清点状态"
        prop="checkStatus"
        :formatter="formatter"
        width="150"
        header-align="center"
        align="left"
      />
      <el-table-column
        label="入账状态"
        prop="inputStatus"
        :formatter="formatter"
        width="100"
        header-align="center"
        align="left"
      />
      <el-table-column
        label="提交人"
        prop="payer"
        width="100"
        header-align="center"
        align="left"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.payer }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="实际缴款人"
        prop="actPayerName"
        width="100"
        header-align="center"
        align="left"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.actPayerName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createAt"
        width="200"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createAt }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageNoSize"
      size="small"
      @pagination="getList"
    />
    <el-dialog
      :title="dialogTitleMap[detailType]"
      :visible.sync="dialogFormVisible"
      top="3vh"
      width="80%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center
    >
      <el-form :model="temp" label-position="right" label-width="120px">
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="企业名称" prop="companyName">
              {{ temp.companyName }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="站点名称" prop="stationName">
              {{ temp.stationName }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="订单编号" prop="orderNo">
              {{ temp.orderNo }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="箱包编号" prop="qrcodeNo">
              {{ temp.qrcodeNo }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="箱包id" prop="id">
              {{ temp.id }}
            </el-form-item>
          </el-col>
          <!-- <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="订单编号" prop="orderNo">
              {{ temp.orderNo }}
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="箱包状态" prop="bagsStatus">
              <el-select
                v-model="temp.bagsStatus"
                disabled
                placeholder="请选择箱包状态"
              >
                <el-option
                  v-for="item in bagsStatusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="创建时间" prop="createAt">
              {{ temp.createAt }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="款项来源" prop="counterparty">
              {{ temp.counterparty }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="清点状态" prop="checkStatus">
              <el-select
                v-model="temp.checkStatus"
                disabled
                placeholder="请选择清点状态"
              >
                <el-option
                  v-for="item in checkStatusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="temp.checkDate" :xs="12" :sm="8" :lg="8">
            <el-form-item label="清点日期" prop="checkDate">
              {{ temp.checkDate }}
            </el-form-item>
          </el-col>
          <el-col v-if="temp.checkUserName" :xs="12" :sm="8" :lg="8">
            <el-form-item label="清点人" prop="temp.checkUserName">
              {{ temp.checkUserName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="12" :sm="6" :lg="6">
            <el-form-item label="原金额" prop="inputAmount">
              {{ temp.inputAmount != null ? temp.inputAmount.toFixed(2) : "" }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="6" :lg="6">
            <el-form-item label="清点金额" prop="checkAmount">
              {{ temp.checkAmount != null ? temp.checkAmount.toFixed(2) : "" }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="6" :lg="6">
            <el-form-item label="假币金额" prop="counterfeitAmount">
              {{
                temp.counterfeitAmount != null
                  ? temp.counterfeitAmount.toFixed(2)
                  : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="6" :lg="6">
            <el-form-item label="差额" prop="gapAmount">
              {{ temp.gapAmount != null ? temp.gapAmount.toFixed(2) : "" }}
            </el-form-item>
          </el-col>
          <el-col v-if="temp.backAmount" :xs="12" :sm="6" :lg="6">
            <el-form-item label="退回金额" prop="backAmount">
              {{ temp.backAmount != null ? temp.backAmount.toFixed(2) : "" }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="入账状态" prop="inputStatus">
              <el-select
                v-model="temp.inputStatus"
                disabled
                placeholder="请选择入账状态"
              >
                <el-option
                  v-for="item in inputStatusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item
              v-if="temp.accountDate"
              label="记账日期"
              prop="accountDate"
            >
              {{ temp.accountDate }}
            </el-form-item>
          </el-col>
          <el-col v-if="temp.entryAmount" :xs="12" :sm="8" :lg="8">
            <el-form-item label="入账金额" prop="entryAmount">
              {{ temp.entryAmount }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="户名" prop="accountName">
              {{ temp.accountName }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="8" :lg="8">
            <el-form-item label="账号" prop="accountNo">
              {{ temp.accountNo }}
            </el-form-item>
          </el-col>
          <el-col v-if="temp.subAccountNo" :xs="12" :sm="8" :lg="8">
            <el-form-item label="子账号" prop="subAccountNo">
              {{ temp.subAccountNo }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="12" :sm="6" :lg="6">
            <el-form-item label="提交人" prop="payer">
              {{ temp.payer }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="6" :lg="6">
            <el-form-item label="提交人联系方式" prop="payerPhoneNum">
              {{ temp.payerPhoneNum }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="6" :lg="6">
            <el-form-item label="实际缴款人" prop="actPayerName">
              {{ temp.actPayerName }}
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="6" :lg="6">
            <el-form-item label="实际缴款人联系方式" prop="actPayerPhoneNum">
              {{ temp.actPayerPhoneNum }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="block">
        <el-timeline>
          <el-timeline-item
            v-for="(item, index) in timeline"
            :key="index"
            :timestamp="item.createAt"
          >
            <p>
              {{ item.creatorName }}
              <span v-html="item.remark" />
            </p>
          </el-timeline-item>
        </el-timeline>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 关闭 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSmskBagsInfoList, getSmskBagTimeline } from '@/api/smsk/bagsInfo'
import { setOptionData, valueFormate } from '@/utils/dict'
import Pagination from '@/components/Pagination'

export default {
  name: 'SmskBagsInfo',
  components: { Pagination },
  data() {
    return {
      bagsStatusOptions: [],
      checkStatusOptions: [],
      inputStatusOptions: [],
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        companyName: '',
        stationName: '',
        bagsStatus: '',
        orderNo: '',
        payer: '',
        createAt: [],
        sortField: undefined,
        sortOrder: undefined
      },
      dialogFormVisible: false,
      detailType: '',
      dialogTitleMap: {
        detail: '箱包详情'
      },
      temp: {
      },
      timeline: []
    }
  },
  created() {
    // 加载下拉数据
    const optionsMap = new Map()
    optionsMap.set('SMSK_BAGS_STATUS', this.bagsStatusOptions)
    optionsMap.set('SMSK_CHECK_STATUS', this.checkStatusOptions)
    optionsMap.set('SMSK_INPUT_STATUS', this.inputStatusOptions)
    setOptionData(optionsMap)
    this.getList()
  },
  methods: {
    formatter(row, column, cellValue, index) {
      return valueFormate(this, column.property, cellValue)
    },
    getList() {
      this.listLoading = true
      getSmskBagsInfoList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }, response => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleReset() {
      this.listQuery = {
        companyName: '',
        stationName: '',
        bagsStatus: '',
        orderNo: '',
        payer: '',
        createAt: []
      }
    },
    handleDetail(row) {
      this.temp = Object.assign({}, row)
      getSmskBagTimeline({ bagsId: row.id }).then(res => {
        this.timeline = res.data
      })
      this.detailType = 'detail'
      this.dialogFormVisible = true
    },
    sortChange(data) {
      const { prop, order } = data
      this.sortByField(prop, order)
    },
    sortByField(prop, order) {
      if (order === 'ascending') {
        this.listQuery.sortField = prop
        this.listQuery.sortOrder = 'asc'
      } else if (order === 'descending') {
        this.listQuery.sortField = prop
        this.listQuery.sortOrder = 'desc'
      } else {
        this.listQuery.sortField = undefined
        this.listQuery.sortOrder = undefined
      }
      this.handleFilter()
    }
  }
}
</script>
